#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstring>
#include<string.h>
using namespace std;
typedef struct
{
    int weight;
    int parent, lchild, rchild;
}HTNode, * HuffmanTree; // 哈夫曼树
typedef char** HuffmanCode; //哈夫曼编码

void Select(HuffmanTree HT, int len, int& s1, int& s2)
{
    //通过flag和parent来实现求最小值s1和次小值s2，避免使用中间变量最小值min
        int i, flag = 0;
    for (i = 1; i <= len; i++)
    {
        if (flag == 0 && HT[i].parent == 0)
        {
            s1 = i; flag = 1;
        }
        if (flag == 1 && HT[i].weight < HT[s1].weight && HT[i].parent == 0)
        {
            s1 = i;
        }
    }
    for (i = 1; i <= len; i++)
    {
        //这里i不能和s1相等
        if (flag == 1 && HT[i].parent == 0 && i != s1)
        {
            s2 = i; flag = 0;
        }
        if (flag == 0 && HT[i].weight < HT[s2].weight && HT[i].parent == 0 && i != s1)
        {
            s2 = i;
        }
    }
}

//用算法5.10构造赫夫曼树
void CreatHuffmanTree(HuffmanTree& HT, int n)
{
    //构造赫夫曼树HT
    int m, s1, s2, i;
    if (n <= 1)return;
    m = 2 * n - 1;
    HT = new HTNode[m + 1];        //0号单元未用，所以需要动态分配m+1个单元，HT[m]表示根结点
    for (i = 1; i <= m; ++i)           //将1~m号单元中的双亲、左孩子，右孩子的下标都初始化为0
    {
        HT[i].parent = 0;  HT[i].lchild = 0;  HT[i].rchild = 0;
    }
    //cout<<"请输入叶子结点的权值：\n";
    for (i = 1; i <= n; ++i)           //输入前n个单元中叶子结点的权值
        cin >> HT[i].weight;
    /*――――――――――初始化工作结束，下面开始创建赫夫曼树――――――――――*/
    for (i = n + 1; i <= m; ++i)
    {  //通过n-1次的选择、删除、合并来创建赫夫曼树
        Select(HT, i - 1, s1, s2);
        //在HT[k](1≤k≤i-1)中选择两个其双亲域为0且权值最小的结点,
        // 并返回它们在HT中的序号s1和s2
        HT[s1].parent = i;
        HT[s2].parent = i;
        //得到新结点i，从森林中删除s1，s2，将s1和s2的双亲域由0改为i
        HT[i].lchild = s1;
        HT[i].rchild = s2;                          //s1,s2分别作为i的左右孩子
        HT[i].weight = HT[s1].weight + HT[s2].weight;  //i 的权值为左右孩子权值之和
    }                                              //for
}                                                 // CreatHuffmanTree

void CreatHuffmanCode(HuffmanTree HT, HuffmanCode& HC, int n)
{
    //char cd[];
    //从叶子到根逆向求每个字符的赫夫曼编码，存储在编码表HC
    int i, start, c, f;
  
    HC = new char* [n + 1];//分配n个字符编码的头指针矢量
    HC = (char**)malloc((n + 1) * sizeof(char*));
   
    char* cd = new char[n];
    //cd = (char*)malloc(n * sizeof(char));//分配临时存放编码的动态数组空间

    cd[n - 1] = '\0';
    for (i = 1; i <= n; i++) /// 从叶子结点开始向上回溯，直到根结点
    {
        start = n - 1;
        c = i;
        f = HT[i].parent;
        while (f)
        {
            --start;
            if (HT[f].lchild == c)
            {
                cd[start] = '0';
            }
            else
            {
                cd[start] = '1';
            }
            c = f;
            f = HT[f].parent;
        }
        HC[i] = new char[n - start];
       // HC[i] = (char*)malloc((n - start) * sizeof(char));       // 为第i 个字符编码分配空间
        strcpy(HC[i], &cd[start]);
    }
    delete cd;
}


int countWPL(HuffmanTree HT, int n)  //计算树的带树路径长度
{
     //通过计算内部节点权值求树的带权路径
    
    int i, sum = 0;
    for (i = n + 1; i <= 2 * n - 1; i++)
    {
        sum += HT[i].weight;
    }
    return sum;
}

int main()
{
    HuffmanTree HT;
    HuffmanCode HC;
    int n;
    cin >> n;                                          //输入赫夫曼树的叶子结点个数
    CreatHuffmanTree(HT, n);
    CreatHuffmanCode(HT, HC, n);
    for (int i = 1; i <= n; i++)
    {
        //printf("权值为：%d", HT[i].weight);
        //printf("的编码为：%s\n", HC[i]);
        printf("%s\n", HC[i]);
    }
    //cout << countWPL(HT, n);
    return 0;
}